package cn.wanho.service.impl;

import cn.wanho.dao.CmsExamCountMapper;
import cn.wanho.dao.CmsExamMapper;
import cn.wanho.dao.CmsMultileChoiceQuestionMapper;
import cn.wanho.dto.CmsExam;
import cn.wanho.rvo.*;
import cn.wanho.service.CmsExamService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CmsExamServiceImpl implements CmsExamService {

    @Resource
    private CmsMultileChoiceQuestionMapper cmsMultileChoiceQuestionMapper;

    @Resource
    private CmsExamMapper cmsExamMapper;

    @Resource
    private CmsExamCountMapper cmsExamCountMapper;

    private final String EXAM = "Answer:exam";
    private final String FINALEXAM = "finish:exam";

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 创建试卷
     * @param cmsExam
     * @return
     */
    @Override
    public Integer insert(CmsExam cmsExam) {
        int i = cmsExamMapper.insert(cmsExam);
        return i;
    }

    /**
     * 保存当前答题（redis存储）
     * @param questionId
     * @param answerContent
     * @param paperId
     * @param userId
     * @return
     */
   /* @Override
    public ResultVO saveExam(String questionId, String answerContent, String paperId, Integer userId) {
        String key = EXAM+userId+paperId+":choose";
        String key1 = EXAM+userId+paperId+":blank";
        String key2 = EXAM+userId+paperId+":judge";
        String key3 = EXAM+userId+paperId+":program";
        //试卷所有题
        List<Object> list = new ArrayList<>();
        //选择题
        List<ChooseVO> chooses = cmsExamMapper.choose(Integer.valueOf(paperId));
        if (!chooses.isEmpty()) {
            for (ChooseVO choose : chooses) {
                RedisExamVO redisExamVO = new RedisExamVO(userId, choose.getSort(), choose.getId(), answerContent);
                redisTemplate.opsForZSet().add(key,redisExamVO,choose.getSort());
                list.add(choose);
            }
        }
        //填空题
        List<QuestionVO> blank = cmsExamMapper.blank(Integer.valueOf(paperId));
        if (!blank.isEmpty()){
            for (QuestionVO questionVO : blank) {
                RedisExamVO redisExamVO = new RedisExamVO(userId, questionVO.getSort(), questionVO.getId(), answerContent);
                redisTemplate.opsForZSet().add(key1, redisExamVO,questionVO.getSort());
                list.add(questionVO);
            }
        }
        //判断题
        List<QuestionVO> judge = cmsExamMapper.judge(Integer.valueOf(paperId));
        if (!judge.isEmpty()){
            for (QuestionVO questionVO : judge) {
                RedisExamVO redisExamVO = new RedisExamVO(userId, questionVO.getSort(), questionVO.getId(), answerContent);
                redisTemplate.opsForZSet().add(key2, redisExamVO,questionVO.getSort());
                list.add(questionVO);
            }
        }
        //程序题
        List<QuestionVO> program = cmsExamMapper.program(Integer.valueOf(paperId));
        if (!program.isEmpty()){
            for (QuestionVO questionVO : program) {
                RedisExamVO redisExamVO = new RedisExamVO(userId, questionVO.getSort(), questionVO.getId(), answerContent);
                redisTemplate.opsForZSet().add(key3, redisExamVO,questionVO.getSort());
                list.add(questionVO);
            }
        }
        if (!list.isEmpty()){
            return ResultVO.success(ResultEnum.QUESTION_SAVE_SUCCESS.getResCode(), ResultEnum.QUESTION_SAVE_SUCCESS.getResMsg(), list);
        }else {
            return ResultVO.failure(ResultEnum.QUESTION_SAVE_FAILURE.getResCode(), ResultEnum.QUESTION_SAVE_FAILURE.getResMsg());
        }
    }*/

    /**
     * 考试结束提交试卷
     * @param paperId
     * @param userId
     * @return
     */
  /*  @Override
    public ResultVO commit( String paperId, String userId) {
        int count = 0;
        int fraction = 0;
        List<RedisExamVO> keys = (List<RedisExamVO>) redisTemplate.keys(EXAM+userId+paperId+"choose");
        List<ChooseVO> keys1 = (List<ChooseVO>) redisTemplate.keys("cms:exam:" + userId + ":" + paperId + ":choose");
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < keys.size(); i++) {
            String answerContent = keys.get(i).getAnswerContent();
            String answer = keys1.get(i).getAnswer();
            if (answer.equals(answerContent)) {
                count++;
            }
            fraction = keys1.get(i).getValue() * count;
            keys.get(i).setScore(fraction));
        }
        list.add(count);
        return ResultVO.success(ResultEnum.TEST_COMMIT_SUCCESS.getResCode(), ResultEnum.TEST_COMMIT_SUCCESS.getResMsg(),list);
    }*/

}
